package com.example.sample.activity;

import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieCompositionFactory;
import com.airbnb.lottie.LottieListener;
import com.airbnb.lottie.LottieTask;
import com.example.sample.R;
import com.example.sample.util.MPermissionDelegate;

import net.liteheaven.xblib.common.log.XLog;
import net.liteheaven.xblib.common.ui.ToastUtil;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.GenericLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static com.airbnb.lottie.LottieDrawable.RESTART;

public class LottieActivity extends AppCompatActivity {

    public static final String ASSET = "home.json";
    private LottieComposition composition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lottie);

        findViewById(R.id.vibrate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final long[] pattern = {10, 200, 200, 200};
                Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    VibrationEffect ve = VibrationEffect.createWaveform(pattern, -1);
                    vibrator.vibrate(ve);
                }else{
                    vibrator.vibrate(pattern, -1);
                }
            }
        });

        final LottieAnimationView lottie = findViewById(R.id.lottie);
        lottie.setImageDrawable(getResources().getDrawable(R.drawable.tab_btn_home));
        lottie.addAnimatorListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //lottie.setImageDrawable(getResources().getDrawable(R.drawable.tab_btn_home));
            }
        });


        lottie.setRepeatCount(0);
        lottie.setRepeatMode(RESTART);

        // 方法1
        //lottie.setAnimation("home.json");

        //lottie.playAnimation();

        LottieTask<LottieComposition> lottieTask =  LottieCompositionFactory.fromAsset(this, ASSET);
        lottieTask.addListener(new LottieListener<LottieComposition>() {
            @Override
            public void onResult(LottieComposition result) {
                LottieActivity.this.composition = result;
            }
        });

        findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lottie.setComposition(composition);
                lottie.playAnimation();
            }
        });

        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_PHONE_STATE}, 0);

        String serial = getSerial(this);
        //serial = Build.SERIAL;
        ToastUtil.show(this, serial);
    }

    @SuppressLint("MissingPermission")
    public static String getSerial(Context context) {
        String serial;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                serial = Build.getSerial();
            }else{
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1){
                    // 这俩版这个字段虽然废弃但还有值
                    serial = Build.SERIAL;
                }else{
                    // P以后SERIAL变为unknown，未来也许字段就没了，自己填
                    serial = "unknown";
                }
            }
        }else{
            serial = Build.SERIAL;
        }
        return serial;
    }

    public static class TriangleView extends View{

        private Bitmap bitmap;
        private Paint paint = new Paint();

        public TriangleView(Context context) {
            this(context, null);
        }

        public TriangleView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public TriangleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(){
            Context context = getContext();
            if (context instanceof LifecycleOwner){
                LifecycleOwner lifecycleOwner = (LifecycleOwner) context;
                final Lifecycle lifecycle = lifecycleOwner.getLifecycle();
                lifecycle.addObserver(new GenericLifecycleObserver() {
                    @Override
                    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
                        if (event == Lifecycle.Event.ON_DESTROY){
                            lifecycle.removeObserver(this);
                            release();
                        }
                    }
                });
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawBitmap(getBitmap(), 0, 0, paint);
        }

        private Bitmap getBitmap(){
            if (bitmap == null){
                int w = getMeasuredWidth();
                int h = getMeasuredHeight();
                bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(bitmap);

                // 边长
                int bl = 25;

                // 区域
                int t = 0;
                int b = t + bl;
                int r = w;
                int l = r - bl;

                {
                    Paint paint = new Paint();
                    paint.setColor(Color.parseColor("#E3E3E3"));
                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    paint.setStrokeWidth(1);

                    Path path = new Path();
                    path.moveTo(l, t);
                    path.lineTo(r, b);
                    path.lineTo(l, b);
                    path.lineTo(l, t);
                    path.close();

                    canvas.drawPath(path, paint);
                }

                {
                    Paint paint = new Paint();
                    paint.setColor(Color.WHITE);
                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    paint.setStrokeWidth(1);

                    Path path = new Path();
                    path.moveTo(0, 0);
                    path.lineTo(l, t);
                    path.lineTo(l, b);
                    path.lineTo(r, b);
                    path.lineTo(w, h);
                    path.lineTo(0, h);
                    path.lineTo(0, 0);
                    path.close();

                    canvas.drawPath(path, paint);
                }
            }
            return bitmap;
        }

        private void release(){
            if (bitmap != null){
                bitmap.recycle();
                bitmap = null;
            }
        }
    }
}
